package com.cms.controller.admin;

import com.cms.domain.Feedback;
import com.cms.entity.Guestbook;
import com.cms.service.GuestbookService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 留言
 * 
 * 
 * 
 */
@Controller("adminGuestbookController")
@RequestMapping("/admin/guestbook")
public class GuestbookController extends BaseController {

	@Autowired
	private GuestbookService guestbookService;


	/**
	 * 列表
	 */
	@RequestMapping
	public String index(String name,Integer pageNumber,ModelMap modelMap) {
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		modelMap.addAttribute("page", guestbookService.findPage(pageNumber,PAGE_SIZE,getCurrentSite().getId()));
		modelMap.addAttribute("name", name);
		return getView("guestbook/index");
	}
	
	/**
	 * 回复
	 */
	@RequestMapping("/reply")
	public String reply(Integer id,ModelMap modelMap){
		Guestbook guestbook = guestbookService.getById(id);
		modelMap.addAttribute("guestbook", guestbook);
		return getView("guestbook/reply");
	}
	
	/**
	 * 修改回复
	 */
	@ResponseBody
	@RequestMapping("/updateReply")
	public Feedback updateReply(String replyContent, Integer guestbookId){
		Guestbook guestbook = guestbookService.getById(guestbookId);
		guestbook.setReplyContent(replyContent);
		guestbookService.updateById(guestbook);
		return Feedback.success(new HashMap<>());
	}
	
	/**
	 * 修改状态
	 */
	@ResponseBody
	@RequestMapping("/updateStatus")
	public Feedback updateStatus(Boolean value,Integer id){
		Integer status = BooleanUtils.toInteger(value);
		Guestbook guestbook = guestbookService.getById(id);
		guestbook.setStatus(status);
		guestbook.setUpdateDate(LocalDateTime.now());
		guestbookService.updateById(guestbook);
		return Feedback.success(new HashMap<>());
	}


	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			guestbookService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

}